//
//  HJJReadViewController.m
//  Pianke
//
//  Created by 黄俊杰 on 15/12/22.
//  Copyright © 2015年 dada. All rights reserved.
//

#import "HJJReadViewController.h"
#import "WWYReadListModel.h"
#import "WWYLIstCollectionViewCell.h"
#import "WWYReadDetailViewController.h"

#import "SDCycleScrollView.h"
#import "LDJ_HomeDetailViewController.h"


#define ITEM_WIDTH (SCREEN_WIDTH - 20) / 3
#define ITEM_HEIGHT (SCREEN_HEIGHT - 66 - 210) / 3



@interface HJJReadViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, SDCycleScrollViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *allArray;

@property (nonatomic, strong) SDCycleScrollView *cycleScrollView;

@property (nonatomic, strong) NSMutableArray *imgArray;

@property (nonatomic, strong) NSMutableArray *urlArray;

//轮播图
//@property (nonatomic, strong)


@end

@implementation HJJReadViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [UINib nibWithNibName:@"ListCell" bundle:[NSBundle mainBundle]] ;
    
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    //区缩进量
    flow.sectionInset = UIEdgeInsetsMake(0, 5, 5, 5);
    flow.minimumInteritemSpacing = 5;
    flow.minimumLineSpacing = 5;
    flow.headerReferenceSize = CGSizeMake(0, 0);
    flow.itemSize = CGSizeMake(ITEM_WIDTH, ITEM_HEIGHT);

    
    //collectionView
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flow];
    self.collectionView.backgroundColor = [UIColor grayColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        //66是navigationBar留白..150是轮播图定稿..5是间隙
        
        make.edges.mas_equalTo(UIEdgeInsetsMake(66 + 190 + 5, 0, 0, 0));
    }];
    //注册(xib做的Cell)
    [self.collectionView registerNib:[UINib nibWithNibName:@"ListCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"ListCollectionViewCell"];
    //client=2&auth=B8Y4En3h8f1DC8Z8GcHulTJxQMOuNLCNojtsUT0xcWP22NVFSh9U6fo
    //请求头
    //cookie:PHPSESSID=58ts41v1nohdn89damknm0vdt5

    
    [LDJ_AppTools postUrl:@"http://api2.pianke.me/read/columns" body:@{@"client":@2, @"auth":@"B8Y4En3h8f1DC8Z8GcHulTJxQMOuNLCNojtsUT0xcWP22NVFSh9U6fo"} result:LDJJSON requestStyle:LDJRequestJSON headerFile:@{@"PHPSESSID":@"58ts41v1nohdn89damknm0vdt5"} success:^(id result) {
        

        
        
        self.allArray = [NSMutableArray array];
        for (NSMutableDictionary *temp in [[result valueForKey:@"data"] valueForKey:@"list"]) {
            WWYReadListModel *model = [WWYReadListModel initWithDictionary:temp];
            [self.allArray addObject:model];
        }
        self.imgArray = [NSMutableArray array];
        self.urlArray = [NSMutableArray array];
        for (NSMutableDictionary *temp in [[result valueForKey:@"data"] valueForKey:@"carousel"]) {
            [self.imgArray addObject:[temp valueForKey:@"img"]];
            //处理url
           


            NSArray *array = [[temp valueForKey:@"url"] componentsSeparatedByString:@"/"];
            NSLog(@"heheheheh%@", array);
            [self.urlArray addObject:array[3]];
        }
        //创建轮播图
        [self makeCycleScrollView];
        
        [self.collectionView reloadData];
    } failure:^(NSError *error) {
        NSLog(@"%@", error);
    }];
    //适配
    self.automaticallyAdjustsScrollViewInsets=NO;
}

#pragma mark -创建轮播图
- (void)makeCycleScrollView{
    self.cycleScrollView = [[SDCycleScrollView alloc] init];
    self.cycleScrollView.delegate = self;
    self.cycleScrollView.imageURLStringsGroup = self.imgArray;
    self.cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    self.cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
    [self.view addSubview:self.cycleScrollView];
    [self.cycleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(@66);
        make.right.left.mas_equalTo(@0);
        make.height.mas_equalTo(@190);
        
    }];
}
#pragma mark -轮播图点击时间
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    NSLog(@"lalalalalalalalala%@", self.urlArray[index]);
    LDJ_HomeDetailViewController *hehe = [[LDJ_HomeDetailViewController alloc] init];
    hehe.detaId = self.urlArray[index];
    [self.navigationController pushViewController:hehe animated:YES];
}

#pragma mark -collectionView基本设置
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.allArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifer = @"ListCollectionViewCell";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifer forIndexPath:indexPath];
    cell.backgroundColor = [UIColor yellowColor];
    
    //控件赋值()赋值在这里不知道规不规范
    WWYReadListModel *temp = self.allArray[indexPath.row];
    //自定义cell赋值方法
    [self assignmentCell:temp ForCell:(WWYLIstCollectionViewCell *)cell];
    return cell;
}

#pragma mark -自定义控件赋值方法
- (void)assignmentCell:(WWYReadListModel *)model ForCell:(WWYLIstCollectionViewCell *)cell{
    //获取xib
    //NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"ListCell" owner:nil options:nil];
    //NSLog(@"----------%@" , [array[0] class]);
    [cell.BackView sd_setImageWithURL:[NSURL URLWithString:model.coverimg] placeholderImage:[UIImage imageNamed:@"zanwei"]];
    NSString *name = model.name;
    NSString *enName = model.enname;
    //可变字符串
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:[name stringByAppendingString:enName]];
    [string addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} range:NSMakeRange(name.length, enName.length)];
    cell.Label.attributedText = string;
}

#pragma mark -collectionview点击事件

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    WWYReadDetailViewController *hehe = [[WWYReadDetailViewController alloc] init];
    hehe.model = self.allArray[indexPath.row];
    [self.navigationController pushViewController:hehe animated:YES];
}


//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//    if (indexPath.row != self.allArray.count - 1){
//        return CGSizeMake(100, 120);
//    }else{
//        return CGSizeMake(self.view.frame.size.width - 10, 120);
//    }
//}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
